﻿/*
 * MessagePipe.h
 *
 */
#ifndef __GDK_MESSAGEPIPE_H__
#define __GDK_MESSAGEPIPE_H__

#include <gdk/core/Types.h>
#include <gdk/core/Message.h>
#include <gdk/core/Mutex.h>
#include <gdk/core/Stream.h>
#include <gdk/core/MemoryStream.h>
#include <gdk/core/MessageDispenser.h>
#include <gdk/core/Semaphore.h>


namespace gdk
{


	/////////////////////////////////////////////////////////
	class GDK_CORE_EXPORT MessagePort
	{
	private:
		MessageDispenser<void>	_messageDispenser;
		Mutex					_messageMutex;
		MemoryStream::Handle	_messageBuffer;
		InputStream				_messageBuffer_is;
		OutputStream			_messageBuffer_os;
		Semaphore::Handle		_messageSemaphore;
		Mutex					_cacheMutex;
		MemoryStream::Handle	_cache;
		InputStream				_cache_is;
		OutputStream			_cache_os;

	public:
		MessagePort(void);

		bool pushMessage(const Message::Handle &msg);
		ssize_t procMessage(uint32_t timeout = UINT32_MAX); //返回处理的消息数
		Message::Handle popMessage(void);
		void setMessageSemaphore(Semaphore *semaphore);

		__INLINE__ MessageDispenser<void>& getMessageDispenser(void) { return _messageDispenser; }
		__INLINE__ const MessageDispenser<void>& getMessageDispenser(void) const { return _messageDispenser; }
	};


	/////////////////////////////////////////////////////////
	class GDK_CORE_EXPORT MessagePipe
	{
	private:
		MessagePort		_serverPort; //消息队列(服务端)
		MessagePort		_clientPort; //消息队列(客户端)

	public:
		MessagePipe(void);

	public:
		__INLINE__ MessagePort& getServerPort(void) { return _serverPort; }
		__INLINE__ MessagePort& getClientPort(void) { return _clientPort; }
		__INLINE__ const MessagePort& getServerPort(void) const { return _serverPort; }
		__INLINE__ const MessagePort& getClientPort(void) const { return _clientPort; }
	};




}//namespace gdk


#endif /*__GDK_MESSAGEPIPE_H__*/
